#include "bsp_main.h"   
#include "string.h"
#include "esp_log.h"

static uint8_t flash_status = FLASH_ON;    

void bsp_main_init()
{
    vTaskDelay(500 / portTICK_PERIOD_MS);
    beep_init();
    tm1650_init();
    magnetic_init();
    temp_adc_init();
    
    local_nvs_init();
    blufi_init();
    my_uart0_init();
    mqtt_init();
    vTaskDelay(2000 / portTICK_PERIOD_MS);
    beep_output(BEEP_START);

}


void task_sysmain(void* arg)
{

    sys_main_param_t sys_main_param_old = {
        .sys_countdown = RUN_MIN_MAX*5*60,
        .sys_error_type = ERROR_TYPE_NO_ERROR,
        .sys_mode = MODE_MAX,
        .sys_power = POWER_MAX,
        .sys_runtime = RUN_MIN_MAX,
        .sys_status = SYS_IDLE,
        .sys_temp_protect = TEMP_PROTECT_CLOSE,
        .ifappmqttconnect = MQTT_APP_DISCONNECT,
        .ifblufibleconnect = BLUFI_BLE_DISCONNECT,
    };
    uint8_t flash_status_old = FLASH_ON;     
    uint8_t sys_need_diaplay_flash = 0;  
    uint8_t *queque_get;
    sys_main_param_t* sys_task_param = Get_sys_param_pos();
    send_param_t* sys_send_param = Get_send_param_pos();
    for(;;){
        vTaskDelay(50 / portTICK_PERIOD_MS);
        if(memcmp(sys_task_param, &sys_main_param_old, sizeof(sys_main_param_t)) != 0){
           sys_need_diaplay_flash = 1;
              /* MQTT连接状态 */
            if(sys_task_param->ifappmqttconnect != sys_main_param_old.ifappmqttconnect){
                if(sys_task_param->ifappmqttconnect == MQTT_CONNECT_PRIORITY_H){
                    sys_task_param->ifappmqttconnect = MQTT_APP_CONNECT;
                }
                if(sys_task_param->ifappmqttconnect == MQTT_APP_CONNECT){
                    mqtt_send(MQTT_SEND_D2S_QD, NULL);
                    mqtt_send(MQTT_SEND_D2S_GJ, NULL);
                }
            }

            /* BLUFI BLE 连接状态 */
            if(sys_task_param->ifblufibleconnect != sys_main_param_old.ifblufibleconnect){
                if(sys_task_param->ifblufibleconnect >= BLUFI_BLE_CONNECT){
                    if(sys_task_param->ifblufibleconnect == BLE_CONNECT_PRIORITY_H){
                        ble_registration_json_send();
                        sys_task_param->ifblufibleconnect = BLUFI_BLE_CONNECT;
                    }
                }

            }

            /* 温度保护 */
            if(sys_task_param->sys_temp_protect != sys_main_param_old.sys_temp_protect){
                // if(sys_task_param->sys_temp_protect == TEMP_PROTECT_CLOSE){
                //     magnetic_output(sys_task_param->sys_mode, sys_task_param->sys_power);
                // }else{
                //     magnetic_pause_or_stop(COIL_STOP_MOTOR_RUN);
                // }
            }

            /* 模式 */
            if(sys_task_param->sys_mode != sys_main_param_old.sys_mode){
                sys_task_param->sys_power = POWER_1;
                
            }

            /* 强度 */
            if(sys_task_param->sys_power != sys_main_param_old.sys_power){
                
            }

            /* 倒计时 */
            if(sys_main_param_old.sys_countdown != sys_task_param->sys_countdown){
                if(sys_task_param->sys_countdown == 0){
                    sys_task_param->sys_status = SYS_STOP;
                    beep_output(BEEP_STOP);
                }
            }
            /* 设定时间 */
            if(sys_main_param_old.sys_runtime != sys_task_param->sys_runtime){
                sys_task_param->sys_countdown = sys_task_param->sys_runtime * 5 * 60;
            }
            /* 错误编码 */
            if(sys_main_param_old.sys_error_type != sys_task_param->sys_error_type){
                if(sys_task_param->sys_error_type == ERROR_TYPE_NTC_ERROR){
                    
                    sys_task_param->sys_status = SYS_PAUSE;
                    beep_output(BEEP_ERROR);
                }
            }
       
            /* 系统状态 */
            if(sys_task_param->sys_status != sys_main_param_old.sys_status){
                switch(sys_task_param->sys_status){
                    case SYS_RUN_NORMAL:
                        /* 功率打开 */
                        magnetic_output(sys_task_param->sys_mode, sys_task_param->sys_power);
                        sys_task_param->sys_error_type = ERROR_TYPE_NO_ERROR;
                        break;
                    case SYS_STOP:
                        sys_task_param->sys_countdown = sys_task_param->sys_runtime * 5 * 60;
                        magnetic_pause_or_stop(COIL_MOTOR_STOP);
                        break;
                    case SYS_PAUSE:
                        magnetic_pause_or_stop(COIL_MOTOR_STOP);
                        
                        break;
                    default:
                        break;

                }

            }
            sys_send_param->send_mode = sys_task_param->sys_mode;
            sys_send_param->send_strength = sys_task_param->sys_power;
            sys_send_param->send_error_type = sys_task_param->sys_error_type;
            sys_send_param->send_status = sys_task_param->sys_status;
            sys_send_param->send_countdown = sys_task_param->sys_countdown;
            sys_send_param->send_runtime = sys_task_param->sys_runtime*5*60;

            /* 同步数据 */
            memcpy(&sys_main_param_old, sys_task_param, sizeof(sys_main_param_t));
            /* 发送状态 */
            if(sys_task_param->ifappmqttconnect == MQTT_APP_CONNECT){
                mqtt_send(MQTT_SEND_D2U_ZT, sys_send_param);
            }
            if(sys_task_param->ifblufibleconnect >= BLUFI_BLE_CONNECT){
                ble_report_json_send(sys_send_param);
            }
        }



        if((sys_need_diaplay_flash == 1) || (flash_status_old != flash_status)){
            sys_need_diaplay_flash = 0;
            flash_status_old = flash_status;
            queque_get = TM1650_led_datatrans(sys_task_param->sys_status, sys_task_param->sys_countdown, sys_task_param->sys_mode,
                                                    sys_task_param->sys_power, sys_task_param->ifblufibleconnect,
                                                        sys_task_param->ifappmqttconnect, flash_status);
            xQueueSend(display_led_queue, queque_get, 0);

        }
    }

}


IRAM_ATTR bool my_timer_on_alarm_cb(gptimer_handle_t timer, const gptimer_alarm_event_data_t *edata, void *user_ctx)
{
    static uint8_t times = 0;
    sys_main_param_t* timer1_sys_param = Get_sys_param_pos();
    /* 运行计时 */
    if(timer1_sys_param->sys_status == SYS_RUN_NORMAL){
        flash_status = !flash_status;
        if(timer1_sys_param->sys_countdown == 0){
            return 1;
        }else{
            if(++times >= 2){
                timer1_sys_param->sys_countdown--;
                times = 0;
            }
        }
    }else{
        flash_status = FLASH_ON;
    }
    /* mqttkeeplive计时 */
    // if(timer1_sys_param->ifappmqttconnect == MQTT_APP_CONNECT){
    //     mqtt_keepalive_count++;
    //     if(mqtt_keepalive_count >= 2*10){
    //         timer1_sys_param->ifappmqttconnect = MQTT_APP_DISCONNECT;
    //         mqtt_keepalive_count = 0;
    //     }
    // }else{
    //     mqtt_keepalive_count = 0;
    // }


    return 1;
}

IRAM_ATTR bool my_timer_on_alarm_cb2(gptimer_handle_t timer, const gptimer_alarm_event_data_t *edata, void *user_ctx)
{
    sys_main_param_t* timer2_sys_param = Get_sys_param_pos();
    /* 运行计时10ms */
    if(timer2_sys_param->sys_status == SYS_RUN_NORMAL){
        magnetic_pin_toggle(timer2_sys_param->sys_mode, timer2_sys_param->sys_temp_protect);
    }
    return 1;
}




















